/**
 * Copyright (c) 2016-2019 人人开源 All rights reserved.
 *
 * https://www.renren.io
 *
 * 版权所有，侵权必究！
 */

package com.ysd.modules.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ysd.common.exception.RRException;
import com.ysd.common.utils.Constant;
import com.ysd.common.utils.PageUtils;
import com.ysd.common.utils.Query;
import com.ysd.modules.sys.dao.CourseDao;
import com.ysd.modules.sys.dao.SysUserDao;
import com.ysd.modules.sys.dao.SysUserRoleDao;
import com.ysd.modules.sys.entity.SysRoleEntity;
import com.ysd.modules.sys.entity.SysTeacher;
import com.ysd.modules.sys.entity.SysUserEntity;
import com.ysd.modules.sys.service.SysRoleService;
import com.ysd.modules.sys.service.SysTeacherService;
import com.ysd.modules.sys.service.SysUserRoleService;
import com.ysd.modules.sys.service.SysUserService;

import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * 系统用户
 *
 * @author Mark sunlightcs@gmail.com
 */
@Service("sysUserService")
public class SysUserServiceImpl extends ServiceImpl<SysUserDao, SysUserEntity> implements SysUserService {
	@Autowired
	private SysUserRoleService sysUserRoleService;
	@Autowired
	private SysRoleService sysRoleService;
	@Autowired
	SysUserDao userDao;
	@Autowired
	SysTeacherService sysTeacherService;


	@Override
	public PageUtils queryPage(Map<String, Object> params) {
		String username = (String)params.get("username");
		Long createUserId = (Long)params.get("createUserId");
		String schoolId=(String)params.get("schoolId");
		String rolename=(String)params.get("rolename");
		int page2=Integer.parseInt((String) params.get("page")) ;
		int limit=Integer.parseInt((String) params.get("limit"));
		int page=(page2-1)*limit;
		System.out.println("---------------"+createUserId);
//		IPage<SysUserEntity> page = this.page(new Query<SysUserEntity>().getPage(params),
//				new QueryWrapper<SysUserEntity>().like(StringUtils.isNotBlank(username),"username", username)
//				.eq(createUserId != null,"create_user_id", createUserId)
//		);
		List<Map<String,Object>> list=userDao.queryUsers(createUserId,username,schoolId,rolename,page,limit);
		for (Map<String, Object> map : list) {
			 Long userId=(Long)map.get("user_id");
		     List<String> roleNames=sysRoleService.getNameByUserId(Integer.parseInt(String.valueOf(userId)));
		     map.put("roleNames", roleNames.stream().collect(Collectors.joining(",")));
		}
		
		Integer count=userDao.queryUsersCount(createUserId,username,schoolId,rolename);
		//获取用户所属的角色列表
		//List<Long> roleIdList = sysUserRoleService.queryRoleIdList(userId);
		return new PageUtils(list,count,limit,page2);
	}

	@Override
	public List<String> queryAllPerms(Long userId) {
		return baseMapper.queryAllPerms(userId);
	}

	@Override
	public List<Long> queryAllMenuId(Long userId) {
		return baseMapper.queryAllMenuId(userId);
	}

	@Override
	public SysUserEntity queryByUserName(String username) {
		return baseMapper.queryByUserName(username);
	}

	@Override
	@Transactional
	public SysUserEntity saveUser(SysUserEntity user) {
		user.setCreateTime(new Date());
		//sha256加密
		String salt = RandomStringUtils.randomAlphanumeric(20);
		user.setPassword(new Sha256Hash(user.getPassword(), salt).toHex());
		user.setSalt(salt);
		this.save(user);
		
		//检查角色是否越权
		checkRole(user);
		
		//保存用户与角色关系
		sysUserRoleService.saveOrUpdate(user.getUserId(), user.getRoleIdList());
		
		//新建一个教师
		SysTeacher tea=new SysTeacher();
		tea.setPhone(user.getMobile());
		tea.setAccount(user.getAccount());
		tea.setSchoolId(user.getSchoolId());
		tea.setSchoolName(user.getSchoolName());
		int userId=Integer.parseInt(String.valueOf(user.getUserId()) ) ;
		tea.setUserId(userId);
	    tea.setPwd(user.getPassword());
	    tea.setType(1);//后期应该是角色
	    sysTeacherService.AddTeacher(tea);
		
		return user;
	}

	@Override
	@Transactional
	public void update(SysUserEntity user) {
		if(StringUtils.isBlank(user.getPassword())){
			user.setPassword(null);
		}else{
			user.setPassword(new Sha256Hash(user.getPassword(), user.getSalt()).toHex());
		}
		this.updateById(user);
		//修改教师
		SysTeacher tea=new SysTeacher();
		tea.setPhone(user.getMobile());
		tea.setAccount(user.getAccount());
		tea.setSchoolId(user.getSchoolId());
		tea.setSchoolName(user.getSchoolName());
		int userId=Integer.parseInt(String.valueOf(user.getUserId()) ) ;
		tea.setUserId(userId);
	    tea.setPwd(user.getPassword());
	    tea.setType(1);//后期应该是角色
	    sysTeacherService.saveOrUpdate(tea);
	    
		//检查角色是否越权
		checkRole(user);
		
		//保存用户与角色关系
		sysUserRoleService.saveOrUpdate(user.getUserId(), user.getRoleIdList());
	}

	@Override
	public void deleteBatch(Long[] userId) {
		this.removeByIds(Arrays.asList(userId));
	}

	@Override
	public boolean updatePassword(Long userId, String password, String newPassword) {
		SysUserEntity userEntity = new SysUserEntity();
		userEntity.setPassword(newPassword);
		return this.update(userEntity,
				new QueryWrapper<SysUserEntity>().eq("user_id", userId).eq("password", password));
	}
	
	/**
	 * 检查角色是否越权
	 */
	private void checkRole(SysUserEntity user){
		if(user.getRoleIdList() == null || user.getRoleIdList().size() == 0){
			return;
		}
		//如果不是超级管理员，则需要判断用户的角色是否自己创建
		if(user.getCreateUserId() == Constant.SUPER_ADMIN){
			return ;
		}
		
		//查询用户创建的角色列表
		List<Long> roleIdList = sysRoleService.queryRoleIdList(user.getCreateUserId());

		//判断是否越权
		if(!roleIdList.containsAll(user.getRoleIdList())){
			throw new RRException("新增用户所选角色，不是本人创建");
		}
	}
}